<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		
		<!--推荐列表-->
		<view class="sugg-list" v-if="searchList.length!=0">
			<view class="sugg-item" v-for="(item,index) in searchList" :key="index">
				<view class="goods-name" @click="gotoDetail(item)">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!--历史区域-->
		<view class="history-box" v-else>
			<!--标题-->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!--列表-->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in histories" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
				kw : "",
				//搜索建议列表
				searchList:[],
				//搜索历史
				historyList:[]
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		
		methods:{
			//防抖输入
			input(e){
				// 清除 timer 对应的延时器
				clearTimeout(this.timer)
				
				
				//如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
				this.timer = setTimeout(()=>{
					this.kw = e
					this.getSearchList()
					
					//进行去重处理
					const set  = new Set(this.historyList)
					set.delete(this.kw)
					
					set.add(this.kw)
					
					this.historyList = Array.from(set)
					
					//进行持久化处理
					uni.setStorageSync("kw",JSON.stringify(this.historyList))
					
				},500)
			},
			//获取搜索建议列表
			async getSearchList(){
				if(this.kw == ''){
					this.searchList = []
					return
				}
				
				const res =  await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(res.statusCode != 200){
					return uni.showMsg()
				}
				
				
				this.searchList = res.data.message
			},
			//跳转到detail页面
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			},
			//清除历史记录
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			//跳转到goods_list页面
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+kw
				})
			}
		},
		computed:{
			histories(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}

.sugg-list{
	padding: 0 5px;
	
	.sugg-item{
		display: flex;
		align-items: center;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		justify-content:space-between;
		
		.goods-name{
			//不允许跨行
			white-space: nowrap;
			//超出的部分隐藏
			overflow: hidden;
			//超出用...
			text-overflow: ellipsis;
			font-size: 12px;
		}
	}
}

.history-box{
	padding: 0px 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		font-size: 13px;
		align-items: center;
		border-bottom: 1px solid #efefef;
		height: 40px;
	}
	
	.history-list{
		display: flex;
		flex-wrap: wrap;
		.uni-tag{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}

</style>
